package utils;


import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


/**
 * excel导出公共方法
 * @author xuyang
 *
 */
@SuppressWarnings( { "unchecked" } )
public class CommonExcelView extends AbstractExcelView {
	/**
	 * @param params
	 *            传入参数 包含以下数据 fileName String 文件名称 titleCN String[] 导出excel表头中文
	 *            titleMap Map<String, Object> excel表头与字段中名称的对应关系 exportData
	 *            List<Map>需要导出的数据
	 * @param workbook
	 *            excel 标签名
	 * @param request
	 *            request
	 * @param response
	 *            response
	 * @throws Exception
	 *             异常
	 */
	@Override
	protected void buildExcelDocument( Map<String, Object> params, HSSFWorkbook workbook, HttpServletRequest request,
					   HttpServletResponse response ) throws Exception
	{
		String fileName = params.get( "fileName" ) + "";
		String[] titles = (String[])params.get( "titleCN" );
		Map<String, Object>	titleMap	= (Map<String, Object>)params.get( "titleMap" );
		List<Map>		exportDatas	= (List<Map>)params.get( "exportData" );

		HSSFSheet sheet = null;

		int	z		= 1;
		int	rowCount	= 1;
		int	sum		= 0;
		/* 导入数据 */
		for ( Map exportData : exportDatas )
		{
			if ( sum % 5999 == 0 )
			{
				rowCount = 1;
				if ( exportDatas.size() <= 5999 )
				{
					sheet = workbook.createSheet( params.get( "fileName" ) + "" );
				}else{
					sheet = workbook.createSheet( params.get( "fileName" ) + "(" + z + ")" );
				}
				HSSFRow header = sheet.createRow( 0 );
				for ( int i = 0; i < titles.length; i++ )
				{
					header.createCell( i ).setCellValue( titles[i] );
				}
				z++;
			}

			HSSFRow aRow = sheet.createRow( rowCount++ );
			for ( int i = 0; i < titles.length; i++ )
			{
				Object o = exportData.get( titleMap.get( titles[i] ) );
				if ( o != null )
				{
					String classSimpleName = o.getClass().getSimpleName();
					/* TODO more types ... */
					if ( classSimpleName.equals( "Double" ) )
					{
						aRow.createCell( i ).setCellValue( (Double) o );
					} else if ( classSimpleName.equals( "Timestamp" ) || classSimpleName.equals( "Date" ) )
					{
						SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
						aRow.createCell( i ).setCellValue( df.format( o ) );
					} else if ( classSimpleName.equals( "Integer" ) )
					{
						aRow.createCell( i ).setCellValue( (Integer) o );
					} else {
						/* 默认转化为String */
						aRow.createCell( i ).setCellValue( o.toString() );
					}
				}
			}
			sum++;
		}
		String startTime = Time.getTimeHMS();
		response.setContentType( "application/vnd.ms-excel" );
		response.setHeader( "Content-disposition", "attachment;filename=\"" + startTime + ".xls\"" );
		OutputStream outputStream = response.getOutputStream();
		workbook.write( outputStream );
		outputStream.flush();
		outputStream.close();
	}
}
